home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req0 / idcmp.c < prev    next >
C/C++ Source or Header  |  1998-04-05  |  9KB  |  371 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7. #include "arexx.h"
  8. #include "fractal.h"
  9.  
  10. #include<dos/rdargs.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/dos_protos.h>
  16. #include<clib/exec_protos.h>
  17. #include<clib/gadtools_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/intuition_protos.h>
  20.  
  21. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  22. static int  doMenuPick(struct Window*, UWORD);
  23. static int  doARexx(struct RexxMsg*, struct Window*);
  24.  
  25. static void new(struct Window*);
  26.  
  27. static int  isCommand(char*, char*, char*, LONG*);
  28. static void freeCommand(void);
  29.  
  30. /* Our message handling code */
  31. void handleIDCMP()
  32. {
  33.     char* text = "Hello World!";
  34.     int going = TRUE;
  35.     int drawing = FALSE;
  36.     ULONG drawsig, toolsig, arexxsig, gotsig;
  37.     struct Window* drawwin = getDrawWin();
  38.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  39.     arexxsig = getARexxSig();
  40.     while(going)
  41.     {
  42.         struct IntuiMessage* intuimsg;
  43.         /* Only include tool window signal mask if window is open */
  44.         toolsig = getToolSig();
  45.         /* Wait for messages to arrive */
  46.         gotsig = Wait(drawsig | toolsig | arexxsig);
  47.         /* Messages have arrived: loop through all of them */
  48.         /* Check messages from the drawing window first */
  49.         if(gotsig & drawsig)
  50.         {
  51.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  52.             {
  53.                 /* Copy the important bits of the message */
  54.                 ULONG class = intuimsg->Class;
  55.                 UWORD code = intuimsg->Code;
  56.                 WORD mousex = intuimsg->MouseX;
  57.                 WORD mousey = intuimsg->MouseY;
  58.                 /* Reply when finished copying bits from message */
  59.                 GT_ReplyIMsg(intuimsg);
  60.                 /* Act on this message... */
  61.                 switch(class)
  62.                 {
  63.                 case IDCMP_MOUSEBUTTONS:
  64.                     switch(code)
  65.                     {
  66.                     case SELECTDOWN:
  67.                         drawing = TRUE;
  68.                         break;
  69.                     case SELECTUP:
  70.                         drawing = FALSE;
  71.                         break;
  72.                     }
  73.                     /* break; omitted so we draw on click, too */
  74.                 case IDCMP_MOUSEMOVE:
  75.                     if(drawing)
  76.                     {
  77.                         Move(drawwin->RPort, mousex, mousey);
  78.                         Text(drawwin->RPort, text, strlen(text));
  79.                     }
  80.                     break;
  81.                 case IDCMP_MENUPICK:
  82.                     going = doMenuPick(drawwin, code);
  83.                     drawwin = getDrawWin();
  84.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  85.                     break;
  86.                 }
  87.             }
  88.         }
  89.         /* Now check messages from the tool window */
  90.         if(going && (gotsig & toolsig))
  91.         {
  92.             struct Window* toolwin = getToolWin();
  93.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  94.             {
  95.                 /* Copy the important bits of the message */
  96.                 ULONG class = intuimsg->Class;
  97.                 UWORD code = intuimsg->Code;
  98.                 APTR iaddr = intuimsg->IAddress;
  99.                 /* Reply when finished copying bits from message */
  100.                 GT_ReplyIMsg(intuimsg);
  101.                 /* Act on this message... */
  102.                 switch(class)
  103.                 {
  104.                 case IDCMP_CLOSEWINDOW:
  105.                     closeToolWin();
  106.                     /* Update our local toolwin, so we stop loop */
  107.                     toolwin = NULL;
  108.                     uncheckToolBar(drawwin);
  109.                     break;
  110.                 case IDCMP_REFRESHWINDOW:
  111.                     /* You *MUST* remember to ask for and handle these refresh messages */
  112.                     GT_BeginRefresh(toolwin);
  113.                     GT_EndRefresh(toolwin, TRUE);
  114.                     break;
  115.                 case IDCMP_GADGETUP:
  116.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  117.                     break;
  118.                 case IDCMP_MENUPICK:
  119.                     going = doMenuPick(drawwin, code);
  120.                     /* Update our local toolwin, so we stop loop */
  121.                     toolwin = getToolWin();
  122.                     drawwin = getDrawWin();
  123.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  124.                     break;
  125.                 }
  126.             }
  127.         }
  128.         /* Now check messages from the ARexx port */
  129.         if(going && (gotsig & arexxsig))
  130.         {
  131.             struct RexxMsg* msg;
  132.             while(going && (msg = getARexxMsg()))
  133.                 going = doARexx(msg, drawwin);
  134.         }
  135.     }
  136. }
  137.  
  138. /* Process IDCMP_GADGETUP event */
  139. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  140. {
  141.     switch(gad->GadgetID)
  142.     {
  143.     case MYBUT_ID:
  144.         /* Our button was clicked!  Set foreground to next pen colour */
  145.         nextFgPen(drawwin);
  146.         break;
  147.     case MYPAL_ID:
  148.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  149.         setFgPen(drawwin, code);
  150.         break;
  151.     }
  152. }
  153.  
  154. /* Process IDCMP_MENUPICK event */
  155. static int doMenuPick(struct Window* drawwin, UWORD code)
  156. {
  157.     UWORD menuCode, menuNumber, itemNumber;
  158.     /* Loop over all the menu selections in the menu code */
  159.     struct MenuItem* item;
  160.     for(menuCode = code;
  161.             menuCode != MENUNULL;
  162.             menuCode = item->NextSelect)
  163.     {
  164.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  165.         /* Extract the menu number and menu item number from the menu code */
  166.         menuNumber = MENUNUM(menuCode);
  167.         itemNumber = ITEMNUM(menuCode);
  168.         /* Now decide what to do based on what menu item was selected */
  169.         switch(menuNumber)
  170.         {
  171.         case 0:  /* Project menu */
  172.             switch(itemNumber)
  173.             {
  174.             case 0:  /* Load */
  175.                 return load();
  176.             case 1:  /* Save */
  177.                 save();
  178.                 break;
  179.             case 3:  /* New (item 2 is the bar!) */
  180.                 new(drawwin);
  181.                 break;
  182.             case 5:  /* Quit (item 4 is the bar!) */
  183.                 {
  184.                     struct EasyStruct myreq = { sizeof(struct EasyStruct),
  185.                                                                             0,
  186.                                                                             "Exit Confirmation",
  187.                                                                             "Do you really wish to quit?",
  188.                                                                             "Yes|No" };
  189.                     if(EasyRequest(drawwin, &myreq, NULL))
  190.                         return FALSE;
  191.                 }
  192.                 break;
  193.             }
  194.             break;
  195.         case 1:  /* Pen menu */
  196.             switch(itemNumber)
  197.             {
  198.             case 0:  /* Next */
  199.                 nextFgPen(drawwin);
  200.                 break;
  201.             case 1:  /* Prev */
  202.                 prevFgPen(drawwin);
  203.                 break;
  204.             case 3:  /* Reset (item 2 is the bar!) */
  205.                 resetFgPen(drawwin);
  206.                 break;
  207.             }
  208.             break;
  209.         case 2:  /* Tools menu */
  210.             switch(itemNumber)
  211.             {
  212.             case 0:  /* Screen Bar */
  213.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  214.                 break;
  215.             case 1:  /* Tool Bar */
  216.                 /* Do the open or close */
  217.                 if(item->Flags & CHECKED)
  218.                 {
  219.                     /* If the open fails, stop immediately */
  220.                     if(!openToolWin())
  221.                         return FALSE;
  222.                 }
  223.                 else
  224.                     closeToolWin();
  225.                 break;
  226.             case 3:  /* Fractal (item 2 is the bar!) */
  227.                 drawFractal(drawwin);
  228.             }
  229.         }
  230.     }
  231.     /* Keep going */
  232.     return TRUE;
  233. }
  234.  
  235. static void new(struct Window* win)
  236. {
  237.     SetRast(win->RPort, 0);
  238. }
  239.  
  240. /* Our RDArgs structure for use with ReadArgs() */
  241. static struct RDArgs* myrdargs = NULL;
  242.  
  243. int createArgs()
  244. {
  245.     if(myrdargs = AllocDosObject(DOS_RDARGS, NULL))
  246.     {
  247.         /* Disable prompting on stdin when "?" is the argument */
  248.         myrdargs->RDA_Flags = RDAF_NOPROMPT;
  249.         return TRUE;
  250.     }
  251.     else
  252.         printf("Error: could not allocate args for ARexx commands\n");
  253.     return FALSE;
  254. }
  255.  
  256. void freeArgs()
  257. {
  258.     if(myrdargs)
  259.         FreeDosObject(DOS_RDARGS, myrdargs);
  260. }
  261.  
  262. /* The result of a ReadArgs() while parsing commands */
  263. static struct RDArgs* rdargs = NULL;
  264.  
  265. /* Test if a string matches a command, using ReadArgs() */
  266. static int isCommand(char* text, char* comm,
  267.                                          char* templ, LONG* args)
  268. {
  269.     int clen = strlen(comm);
  270.     if(strnicmp(text, comm, clen) == 0)
  271.     {
  272.         /* Is the command followed by some whitespace? */
  273.         if(text[clen] == ' ' || text[clen] == '\t')
  274.         {
  275.             int tlen = strlen(text);
  276.             /* Set up our myrdargs so we can use ReadArgs() */
  277.             myrdargs->RDA_Source.CS_Buffer = text+clen+1;
  278.             myrdargs->RDA_Source.CS_Length = tlen-clen;
  279.             myrdargs->RDA_Source.CS_CurChr = 0;
  280.             myrdargs->RDA_DAList = NULL;
  281.             myrdargs->RDA_Buffer = NULL;
  282.             /* Temporarily end the string with a return... */
  283.             /* (Needed to get ReadArgs() to work properly) */
  284.             text[tlen] = '\n';
  285.             rdargs = ReadArgs(templ, args, myrdargs);
  286.             /* ... now we must reinstate the string's terminator */
  287.             text[tlen] = '\0';
  288.             return rdargs != NULL;
  289.         }
  290.     }
  291.     return NULL;
  292. }
  293.  
  294. static void freeCommand()
  295. {
  296.     if(rdargs)
  297.     {
  298.         FreeArgs(rdargs);
  299.         rdargs = NULL;
  300.     }
  301. }
  302.  
  303. /* The maximum number of arguments for our commands */
  304. #define MAX_ARGS (3)
  305.  
  306. #define COMM_QUIT        "QUIT"
  307.  
  308. #define COMM_NEW        "NEW"
  309.  
  310. #define COMM_PEN        "PEN"
  311. #define TEMPL_PEN        "PEN/N"
  312. enum PEN_ARGS { PEN_PEN };
  313.  
  314. #define COMM_DRAW        "DRAW"
  315. #define TEMPL_DRAW    "X/N,Y/N,TEXT/F"
  316. enum DRAW_ARGS { DRAW_X, DRAW_Y, DRAW_TEXT };
  317.  
  318. /* Process an ARexx message */
  319. static int doARexx(struct RexxMsg* msg, struct Window* drawwin)
  320. {
  321.     int going = TRUE;
  322.     /* By default, our reply will indicate an error */
  323.     LONG rc = 20;
  324.     char* res = NULL;
  325.     char* command = msg->rm_Args[0];
  326.     /* Parse the command */
  327.     if(stricmp(command, COMM_QUIT) == 0)
  328.     {
  329.         going = FALSE;
  330.         /* We recognised the command, so set rc to zero */
  331.         rc = 0;
  332.         res = "Hello Painter is quitting";
  333.     }
  334.     else if(stricmp(command, COMM_NEW) == 0)
  335.     {
  336.         new(drawwin);
  337.         rc = 0;
  338.         res = "Display cleared";
  339.     }
  340.     else
  341.     {
  342.         LONG args[MAX_ARGS];
  343.         int i;
  344.         for(i=0; i<MAX_ARGS; i++)
  345.             args[i] = NULL;
  346.         if(isCommand(command, COMM_PEN, TEMPL_PEN, args))
  347.          {
  348.             /* args[0] holds the pen number to use */
  349.             LONG* nptr = (LONG*)(args[PEN_PEN]);
  350.             setFgPen(drawwin, *nptr);
  351.             rc = 0;
  352.             res = "Pen set";
  353.         }
  354.         else if(isCommand(command, COMM_DRAW, TEMPL_DRAW, args))
  355.          {
  356.             /* args[DRAW_X] and args[DRAW_Y] hold the coordinate */
  357.             /* args[DRAW_TEXT] holds the text to be drawn */
  358.             LONG* xptr = (LONG*)(args[DRAW_X]);
  359.             LONG* yptr = (LONG*)(args[DRAW_Y]);
  360.             char* text = (char*)(args[DRAW_TEXT]);
  361.             Move(drawwin->RPort, *xptr, *yptr);
  362.             Text(drawwin->RPort, text, strlen(text));
  363.             rc = 0;
  364.             res = "Text drawn";
  365.         }
  366.         freeCommand();
  367.     }
  368.     replyARexxMsg(msg, rc, res);
  369.     return going;
  370. }
  371.